package com.abso.sunlight.explorer.viewers;

import java.util.List;

import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.ViewerCell;

import com.abso.sunlight.api.Legislator;
import com.abso.sunlight.explorer.LegislatorSearch;

/**
 * The tree viewer label provider for the Congress Explorer tree.
 */
public class ExplorerTreeLabelProvider extends StyledCellLabelProvider /* implements IColorProvider */{

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
     */
    @Override
    public void update(ViewerCell cell) {
        Object element = cell.getElement();
        if (element instanceof ExplorerTreeNode) {
            ExplorerTreeNode node = (ExplorerTreeNode) element;
            StyledString styledString = null;
            if (node.getData() instanceof LegislatorSearch) {
                LegislatorSearch search = (LegislatorSearch) node.getData();
                styledString = new StyledString(search.getName());
                List<Legislator> result = search.getResult();
                if (result != null) {
                    StringBuffer countText = new StringBuffer();
                    countText.append(" (");
                    countText.append(result.size());
                    countText.append(" legislator");
                    if (result.size() != 1) {
                        countText.append("s");
                    }
                    countText.append(")");
                    styledString.append(countText.toString(), StyledString.COUNTER_STYLER);
                }
            } else {
                styledString = new StyledString(node.getText());
            }
            cell.setText(styledString.toString());
            cell.setStyleRanges(styledString.getStyleRanges());
            cell.setImage(node.getImage());
        }
        super.update(cell);
    }

}
